package com.hly.tool.ui;

import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.util.MapList;
import com.matrixone.apps.framework.ui.UIUtil;
import matrix.db.Context;
import matrix.db.JPO;
import matrix.util.StringList;

import java.util.Map;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * Copyright@ Apache Open Source Organization
 *
 * @Auther: HLY
 * @Date: 2023-03-16-10:10
 * @Description:
 */
public class TableUtil
{
    private final Logger _logger = Logger.getLogger("TableUtil");

    /**
     * @param context
     * @param args
     * @return boolean
     * @Author HLY
     * @CreateTime 2023/3/16
     * @Description: 更新 table 表格 单元格数据 表格列名称是 更新对象的属性
     */
    public boolean updateTableColVal(Context context, String[] args)
    {
//        _logger.info(" ------------------------- updateTableColVal ------------------------- ");
        boolean result = false;
        try
        {
            Map argsMap = JPO.unpackArgs(args);
//            _logger.info("argsMap : " + argsMap);
            Map paramMap = (Map) argsMap.get("paramMap");
//            _logger.info("paramMap : " + paramMap);
            String New_Value = (String) paramMap.get("New Value");
//            _logger.info("New_Value : " + New_Value);
            String objectId = (String) paramMap.get("objectId");
//            _logger.info("objectId : " + objectId);
            Map columnMap = (Map) argsMap.get("columnMap");
//            _logger.info("columnMap : " + columnMap);
            String attr = (String) columnMap.get("name");
//            _logger.info("attr : " + attr);
            DomainObject obj = DomainObject.newInstance(context, objectId);
//            _logger.info("obj : " + obj);
            obj.setAttributeValue(context, attr, New_Value);
//            _logger.info("obj.setAttributeValue(context,attr,New_Value) : " + New_Value);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
//        _logger.info(" ------------------------- updateTableColVal ------------------------- ");
        return result;
    }


    /**
     * @param context
     * @param args
     * @return StringList
     * @Author HLY
     * @CreateTime 2023/3/16
     * @Description: 处理一列数据
     */
    public StringList getColImageVal(Context context, String[] args)
    {
        StringList result = new StringList();
//        _logger.info(" ------------------------ getColImageVal ------------------------ ");
        try
        {
            Map argsMap = JPO.unpackArgs(args);
//            _logger.info("argsMap : "+argsMap);
            Map columnMap = (Map) argsMap.get("columnMap");
            String attrName = (String) columnMap.get("name");
//            _logger.info("attrName : " + attrName);
            MapList objectList = (MapList) argsMap.get("objectList");
//            _logger.info("objectList : " + objectList);
            for (int i = 0; i < objectList.size(); i++)
            {
                Map map_i = (Map) objectList.get(i);
//                _logger.info("map_i : " + map_i);
                String image_i = (String) map_i.get("image");
                if (UIUtil.isNotNullAndNotEmpty(image_i))
                {
                    result.add("<img src=\"../common/" + image_i + "\"/>");
                } else
                {
                    result.add("");
                }
            }
        } catch (Exception e)
        {
            throw new RuntimeException(e);
        }
//        _logger.info("result : " + result);
//        _logger.info(" ------------------------ getColImageVal ------------------------ ");
        return result;
    }
}
